﻿using FineUI;
using SysManage.BLL;
using System;
using System.Data;
using System.Linq;
using ZHDJ.Core.Caching;
using ZHDJ.Data.Models.Forum;
using ZHDJ.WebPage;

namespace SysManage.Module.Forum
{
    public partial class ForumList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree();
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
            //CheckPowerWithButton("PartyOrgBranchAdd", btnNew);
            //CheckPowerWithButton("PartyOrgBranchDelete", btnDeleteSelected);

            btnAdd.OnClientClick = Window1.GetShowReference("ForumEdit.aspx", "新增");
            ResolveDeleteButtonForGrid(btnDeleteSelected, Grid2, "确定要从删除选中的{0}项记录吗？");

            // 每页记录数
            Grid2.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();

            BindGrid();
        }

        private void BindTree()
        {
            TreeControlBusiness tree = new TreeControlBusiness("ID", "ParentID", "Title", true, CacheKeys.Cache_ForumType_Tree);
            tree.EnableClickEvent = true;
            tree.BindTreeForumType(TreeForumType);
           
            if (TreeForumType.Nodes.Count > 0)
            {
                //展开第一层
                TreeForumType.Nodes[0].Expanded = true;
            }
        }

        private void BindGrid()
        {
            string parentID = hdParentID.Text;

            if (parentID == "" || parentID == "root")
            {
                Grid2.RecordCount = 1;

                Grid2.DataSource = DB.PartyForumTypes.Where(a => a.ParentID == "").OrderBy(a=>a.OrderNo);
                Grid2.DataBind();
            }
            else
            {
                var ds = PartyForumType.GetForumTypeByParentId(parentID, Grid2.PageIndex, Grid2.PageSize);
                // 在查询添加之后，排序和分页之前获取总记录数
                Grid2.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                Grid2.DataSource = ds.Tables[0];
                Grid2.DataBind();
            }
        }
        #endregion

        #region 事件

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid2.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }
        #endregion

        #region Grid1 Events

        protected void Grid1_RowClick(object sender, FineUI.GridRowClickEventArgs e)
        {
            BindGrid();
        }

        #endregion

        #region Grid2 Events
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeForumType_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            hdParentID.Text = e.Node.NodeID;
            BindGrid();
        }

        protected void Grid2_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithLinkButtonField("PartyForumTypeDelete", Grid2, "deleteField");
        }

        protected void Grid2_Sort(object sender, GridSortEventArgs e)
        {
            Grid2.SortDirection = e.SortDirection;
            Grid2.SortField = e.SortField;
            BindGrid();
        }

        protected void Grid2_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            //if (!CheckPower("PartyForumTypeDelete"))
            //{
            //    CheckPowerFailWithAlert();
            //    return;
            //}

            var IDs = GetSelectedDataKeyIDs(Grid2);
            DB.PartyForumTypes
                .Where(u => IDs.Contains(u.ID))
                .ToList()
                .ForEach(u => DB.PartyForumTypes.Remove(u));

            DB.SaveChanges();

            // 清空当前选中的项
            Grid2.SelectedRowIndexArray = null;

            // 重新绑定表格
            BindGrid();
        }

        protected void Grid2_RowCommand(object sender, GridCommandEventArgs e)
        {
            object[] values = Grid2.DataKeys[e.RowIndex];
            string bID = Convert.ToString(values[0]);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                //if (!CheckPower("PartyForumTypeDelete"))
                //{
                //    CheckPowerFailWithAlert();
                //    return;
                //}
                var forumType = DB.PartyForumTypes.FirstOrDefault(u => u.ID == bID);
                DB.PartyForumTypes.Remove(forumType);
                DB.SaveChanges();
                BindGrid();

            }
            var field = Grid2.FindColumn("editField") as WindowField;
            if (field != null)
            {
                field.DataIFrameUrlFormatString = "ForumEdit.aspx?id={0}&BranchID=" + BranchID;
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindTree();
            BindGrid();
        }

        #endregion
    }
}